package com.xuelang.pipeline.worker.entry.namespace;

import com.xuelang.pipeline.worker.app.namespace.NamespaceService;
import com.xuelang.pipeline.worker.client.common.result.ApiResult;
import com.xuelang.pipeline.worker.client.namespace.INamespaceService;
import com.xuelang.pipeline.worker.client.namespace.dto.NamespaceDTO;
import com.xuelang.pipeline.worker.client.namespace.dto.NamespaceQueryDTO;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.openapi.models.V1NamespaceList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liuyuan.ly
 * @since 2024/5/16 17:06
 */
@RestController
@RequestMapping("/pipeline/worker/")
@Slf4j
public class NamespaceController implements INamespaceService {

    @Autowired
    private NamespaceService namespaceService;

    @Override
    public ApiResult<List<NamespaceDTO>> getNamespace(NamespaceQueryDTO queryDTO) {
        V1NamespaceList v1NamespaceList = namespaceService.getNamespace(queryDTO);
        List<NamespaceDTO> namespaceList = new ArrayList<>();
        v1NamespaceList.getItems().forEach(v1Namespace -> {
            NamespaceDTO dto = new NamespaceDTO();
            dto.setNamespaceName(Objects.requireNonNull(v1Namespace.getMetadata()).getName());
            namespaceList.add(dto);
        });
        return ApiResult.success(namespaceList);
    }

    @Override
    public ApiResult<Boolean> createNamespace(NamespaceQueryDTO queryDTO) {
        log.info("接收创建ns接口，ns为{}", queryDTO.getNamespaceName());
        V1Namespace v1Namespace = namespaceService.createNamespace(queryDTO);
        return ApiResult.success(true);
    }

    @Override
    public ApiResult<Boolean> deleteNamespace(NamespaceQueryDTO queryDTO) {
        return ApiResult.success(namespaceService.deleteNamespace(queryDTO));
    }

}
